//
//  FFShareManager.m
//  Common
//
//  Created by IT.Bear on 16/4/8.
//  Copyright © 2016年 IT.Bear. All rights reserved.
//

#import "FFShareManager.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#import "MobClick.h"


@interface FFShareManager ()
@property (nonatomic,copy)shareResultCallback callback;
@property (nonatomic,copy)clickPlatformCallback platformcallback;
@property (nonatomic,strong)NSArray *shareToSnsNames;
@property (nonatomic,strong)NSString *shareUrl;
@property (nonatomic,strong)NSString *shareText;
@property (nonatomic,strong)NSString *shareTitle;

@end
@implementation FFShareManager

DECLARE_SINGLETON(FFShareManager)

- (id)init {
    self = [super init];
    if (self) {
        
        //channelId为nil或@""时推广渠道默认会被当作@"App Store"渠道。
        UMConfigInstance.appKey = UMENG_APPKEY;
        UMConfigInstance.ePolicy = BATCH;
        
        //打开调试日志
        [[UMSocialManager defaultManager] openLog:YES];
        
        //设置友盟appkey
        [[UMSocialManager defaultManager] setUmSocialAppkey:UMENG_APPKEY];
        //设置QQ的应用Id，和分享url
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPID appSecret:QQAPPKey redirectURL:@"www.fashionfinger.com"];
        //设置微信AppId，设置分享url
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WECHATKEY appSecret:WECHATSECRET redirectURL:@"www.fashionfinger.com"];
        //设置支持没有QQ客户端情况下使用SSO授权
        [[UMSocialQQHandler defaultManager] setSupportWebView:YES];
        //打开新浪微博的SSO开关
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:SINAKEY  appSecret:SINASECRET redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
        
    }
    return self;
}

//回调分享成功or失败
- (void)shareToSnsSheetView:(UIViewController *)controller
                 shareTitle:(NSString *)shareTitle
                  shareText:(NSString *)shareText
                 shareImage:(UIImage *)shareImage
        shareToPlatformType:(UMSocialPlatformType)shareToPlatformType
                   shareUrl:(NSString *)shareUrl
                   callBack:(shareResultCallback)callBack
{
    self.callback = callBack;
    [self shareToSnsSheetView:controller shareTitle:shareTitle shareText:shareText shareImage:shareImage shareToPlatformType:shareToPlatformType shareUrl:shareUrl];
}


//回调点击的分享平台
- (void)shareToSnsSheetView:(UIViewController *)controller
                 shareTitle:(NSString *)shareTitle
                  shareText:(NSString *)shareText
                 shareImage:(UIImage *)shareImage
        shareToPlatformType:(UMSocialPlatformType)shareToPlatformType
                   shareUrl:(NSString *)shareUrl
      clickPlatformCallBack:(clickPlatformCallback)callBack
{
    self.platformcallback = callBack;
    [self shareToSnsSheetView:controller shareTitle:shareTitle shareText:shareText shareImage:shareImage shareToPlatformType:shareToPlatformType shareUrl:shareUrl];
}




- (void)shareToSnsSheetView:(UIViewController *)controller
                 shareTitle:(NSString *)shareTitle
                  shareText:(NSString *)shareText
                 shareImage:(UIImage *)shareImage
        shareToPlatformType:(UMSocialPlatformType)shareToPlatformType
                   shareUrl:(NSString *)shareUrl
{
    self.shareUrl = shareUrl;
    self.shareText = shareText;
    self.shareTitle = shareTitle;
    if (self.shareUrl) {
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPID appSecret:QQAPPKey redirectURL:self.shareUrl];
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WECHATKEY appSecret:WECHATSECRET redirectURL:self.shareUrl];
    }
    if (self.shareText.length == 0 || self.shareText == nil) {
        self.shareText = @"OFF";
    }
    
    if (shareToPlatformType == UMSocialPlatformType_Sina) {
//        NSInteger contentLength = self.shareText.length + self.shareUrl.length;
        if (_shareText.length > 90 && self.shareUrl.length) {
            //            NSInteger count = _shareText.length - 100;
            self.shareText = [NSString stringWithFormat:@"%@...%@",[self.shareText substringWithRange:NSMakeRange(0, 90)],self.shareUrl];
        }else if (self.shareUrl.length){
            self.shareText = [NSString stringWithFormat:@"%@%@",self.shareText,self.shareUrl];
        }
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        messageObject.text = self.shareText;
        //分享消息对象设置分享内容对象
        //有图片的时候分享类型为分享图片
        if (shareImage) {
            //创建图片内容对象
            UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
            [shareObject setShareImage:shareImage];
            shareObject.title = self.shareTitle;
            messageObject.shareObject = shareObject;

        }else{
            //无图片的时候分享类型为分享纯文本
            messageObject.title = self.shareTitle;
        }
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:shareToPlatformType messageObject:messageObject currentViewController:controller completion:^(id data, NSError *error) {
            if (error) {
                if (self.callback) {
                    self.callback(NO,nil);
                }
            }else{
                if (self.callback) {
                    self.callback(YES,nil);
                }
            }
        }];
    }else{
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        
        if (!self.shareUrl.length && shareImage) {
            //创建图片内容对象
            UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
            //如果有缩略图，则设置缩略图
            [shareObject setShareImage:shareImage];
            shareObject.title = self.shareTitle;
            messageObject.text = self.shareText;
            //分享消息对象设置分享内容对象
            messageObject.shareObject = shareObject;
        }else{
            //创建网页内容对象
            UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.shareTitle descr:self.shareText thumImage:nil];
            
            //设置网页地址
            shareObject.webpageUrl = self.shareUrl;
            shareObject.title = self.shareTitle;
            shareObject.descr = self.shareText;
            shareObject.thumbImage = shareImage;
            messageObject.text = self.shareText;
            //分享消息对象设置分享内容对象
            messageObject.shareObject = shareObject;
        }
       
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:shareToPlatformType messageObject:messageObject currentViewController:controller completion:^(id data, NSError *error) {
            if (error) {
                if (self.callback) {
                    self.callback(NO,nil);
                }
            }else{
                if (self.callback) {
                    self.callback(YES,nil);
                }
            }
        }];
        
    }
}



////下面得到分享完成的回调
//-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
//{
//    NSLog(@"didFinishGetUMSocialDataInViewController with response is %@",response);
//    NSString *snsName = [[response.data allKeys] objectAtIndex:0];
//    //根据`responseCode`得到发送结果,如果分享成功
//    if(response.responseCode == UMSResponseCodeSuccess)
//    {
//        //得到分享到的微博平台名
//        NSLog(@"share to sns name is %@",snsName);
//
//        if (self.callback) {
//            self.callback(YES,snsName);
//        }
//
//    }else{
//
//        if (self.callback) {
//            self.callback(NO,snsName);
//        }
//    }
//}
//
//-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData{
//
//    if (platformName == UMShareToSina) {
//        [UMSocialData defaultData].extConfig.sinaData.shareText = [NSString stringWithFormat:@"%@ %@",self.shareText,self.shareUrl];
//    }else if (platformName == UMShareToQQ) {
//        [UMSocialData defaultData].extConfig.qqData.url = self.shareUrl;
//    }else if (platformName == UMShareToQzone){
//        [UMSocialData defaultData].extConfig.qzoneData.url = self.shareUrl;
//    }else if (platformName == UMShareToWechatSession){
//        [UMSocialData defaultData].extConfig.wechatSessionData.url = self.shareUrl;
//    }else if (platformName == UMShareToWechatTimeline){
//        [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.shareUrl;
//    }
//    if (self.platformcallback) {
//        self.platformcallback(platformName);
//    }
//}

-(BOOL)isDirectShareInIconActionSheet{
    
    return NO;
}

- (void)dealloc{
    self.callback = nil;
    self.platformcallback = nil;
    self.shareToSnsNames = nil;
    self.shareUrl = nil;
    
}

@end
